using System;
using Common.Enumerations;
using Sanelib.Common.Extensions;
using Sanelib.Common.Helpers;
using Sanelib.Dto;

namespace Common.Commands.AccountCommands
{
    public class AddAccount : AuditedCommand
    {
        public AccountType AccountType { get; set; }
        public string AccountNumber { get; set; }
        public Industry Industry { get; set; }
        public string BillingStreet { get; set; }
        public string BillingCity { get; set; }
        public string BillingState { get; set; }
        public string BillingPostalCode { get; set; }
        public string BillingCountry { get; set; }
        public string ShippingStreet { get; set; }
        public string ShippingCity { get; set; }
        public string ShippingState { get; set; }
        public string ShippingPostalCode { get; set; }
        public string ShippingCountry { get; set; }
        public string Phone { get; set; }
        public string Mobile { get; set; }
        public string Fax { get; set; }
        public string Website { get; set; }
        public string Email { get; set; }

        public override Guid? GetAggregateId()
        {
            return null;
        }

        public override ErrorResult Validate()
        {
            var validationResult = new ErrorResult();

            if (string.IsNullOrWhiteSpace(Name))
                validationResult.AddError("Name", "should not be empty");

            if (!string.IsNullOrWhiteSpace(Name) && Name.Length > 128)
                validationResult.AddError("Name", " entered exceeds the maximum length 128");

            if (AccountType == null)
                validationResult.AddError("Type", "should be selected");

            if (string.IsNullOrWhiteSpace(AccountNumber))
                validationResult.AddError("Account Number", "should not be empty");

            if (!string.IsNullOrWhiteSpace(AccountNumber) && AccountNumber.Length > 32)
                validationResult.AddError("Account Number", " entered exceeds the maximum length 32");

            if (Industry == null)
                validationResult.AddError("Industry", "should be selected");

            if (!string.IsNullOrWhiteSpace(BillingStreet) && BillingStreet.Length > 128)
                validationResult.AddError("Billing Street", " entered exceeds the maximum length 128");

            if (!string.IsNullOrWhiteSpace(BillingCity) && BillingCity.Length > 128)
                validationResult.AddError("Billing City", " entered exceeds the maximum length 128");

            if (!string.IsNullOrWhiteSpace(BillingState) && BillingState.Length > 128)
                validationResult.AddError("Billing State", " entered exceeds the maximum length 128");

            if (!string.IsNullOrWhiteSpace(BillingCountry) && BillingCountry.Length > 128)
                validationResult.AddError("Billing Country", " entered exceeds the maximum length 128");

            if (!string.IsNullOrWhiteSpace(BillingPostalCode) && BillingPostalCode.Length > 16)
                validationResult.AddError("Billing Postal Code", " entered exceeds the maximum length 16");

            if (!string.IsNullOrWhiteSpace(ShippingStreet) && ShippingStreet.Length > 128)
                validationResult.AddError("Shipping Street", " entered exceeds the maximum length 128");

            if (!string.IsNullOrWhiteSpace(ShippingCity) && ShippingCity.Length > 128)
                validationResult.AddError("Shipping City", " entered exceeds the maximum length 128");

            if (!string.IsNullOrWhiteSpace(ShippingState) && ShippingState.Length > 128)
                validationResult.AddError("Shipping State", " entered exceeds the maximum length 128");

            if (!string.IsNullOrWhiteSpace(ShippingCountry) && ShippingCountry.Length > 128)
                validationResult.AddError("Shipping Country", " entered exceeds the maximum length 128");

            if (!string.IsNullOrWhiteSpace(ShippingPostalCode) && ShippingPostalCode.Length > 16)
                validationResult.AddError("Shipping Postal Code", " entered exceeds the maximum length 16");

            if (!string.IsNullOrWhiteSpace(Phone) && Phone.Length > 16)
                validationResult.AddError("Phone", " entered exceeds the maximum length 16");

            if (!string.IsNullOrWhiteSpace(Mobile) && Mobile.Length > 16)
                validationResult.AddError("Mobile", " entered exceeds the maximum length 16");

            if (!string.IsNullOrWhiteSpace(Fax) && Fax.Length > 16)
                validationResult.AddError("Fax", " entered exceeds the maximum length 16");

            if (!string.IsNullOrWhiteSpace(Website) && Website.Length > 128)
                validationResult.AddError("Website", " entered exceeds the maximum length 128");

            if (!string.IsNullOrWhiteSpace(Email) && Email.Length > 128)
                validationResult.AddError("Email", " entered exceeds the maximum length 128");

            if (Email.IsEmpty())
                validationResult.AddError("Email", "should not be empty");
            else
            {
                if (Email.IsNotEmpty() && Formatter.EmailId(Email) != true)
                    validationResult.AddError("Email", "should be correct formate. (xxxxx@xxx.xx)");
            }

            if (!string.IsNullOrWhiteSpace(Description) && Description.Length > 2048)
                validationResult.AddError("Description", " entered exceeds the maximum length 2048");            

            return validationResult;
        }
    }
}